package com.offices.subscribe.services;


import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
public class WxService {
    private static final String Token = "check";

    /**
     *@classname WxService
     *@description :检测发来的signature是否争正确
     * @param timestamp
     * @param nonce
     * @param signature
     *@return :boolean
     *@author :作者
     *@date :2020/04/29 9:33
     */
    public static boolean check(String timestamp, String nonce, String signature) {
        //1）将token、timestamp、nonce三个参数进行字典序排序
        String[] strs = new String[]{Token, timestamp, nonce};
        Arrays.sort(strs);
        //2）将三个参数字符串拼接成一个字符串进行sha1加密
        StringBuffer str = new StringBuffer();
        //拼接字符串
        Arrays.stream(strs).forEach(str::append);
        //加密
        String mysin = sha1(str.toString());
        //3）开发者获得加密后的字符串可与signature对比，标识该请求来源于微信
        return mysin.equalsIgnoreCase(signature);
    }

    /**
     *@classname WxService
     *@description  将结果进行sha1加密
     *@param src
     *@return :java.lang.String
     *@author :prove
     *@date :2020/04/29 9:32
     */
    private static String sha1(String src) {
        StringBuilder stringBuilder = new StringBuilder();
        try {
            //获取加密方式的实例
            MessageDigest messageDigest = MessageDigest.getInstance("sha1");
            //加密
            byte[] digest = messageDigest.digest(src.getBytes());
            //给16进制预留位置
            char[] chars = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
            //处理加密结果,转16进制
            for (byte b:digest) {
                //将加密出来的每个字节都高低四位都与15
                //高四位向右移动四位与15
                stringBuilder.append(chars[b>>4&15]);
                //低四位与15
                stringBuilder.append(chars[b&15]);
            }
            return stringBuilder.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        if (stringBuilder.toString().equals("")) {
            log.warn("转化结果为空");
        }
        return "";
    }

    /**
     *@classname WxService
     *@description :转化接受的xml信息为map格式
     * @param ip
     *@return :java.util.Map<java.lang.String,java.lang.String>
     *@author prove
     *@date 2020/04/29 9:32
     */
    public static Map<String, String> parseRequest(InputStream ip) {
        Map<String, String> map = new HashMap<>();
        SAXReader saxReader = new SAXReader();
        try {
            //读取文档
            Document document = saxReader.read(ip);
            //获取根节点
            Element rootElement = document.getRootElement();
            //获取根节点所有的子节点
            List<Element> elements = rootElement.elements();
            for (Element e:elements) {
                map.put(e.getName(), e.getStringValue());
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        return map;
    }

}
